package com.baizhi.cmfz.service.impl;

import com.baizhi.cmfz.annottation.AddCacheAnnotation;
import com.baizhi.cmfz.annottation.DeleteCacheAnnotation;
import com.baizhi.cmfz.dao.ArticleDao;
import com.baizhi.cmfz.entity.Article;
import com.baizhi.cmfz.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
   @Autowired
    private ArticleDao articleDao;
    @Override
    //@AddCacheAnnotation
    public Map selectByPageArticle(int page, int limit, String name) {
     int offset=(page-1)*limit;
        List<Article> articles = articleDao.selectByPageArticle(offset, limit, name);
        int count = articleDao.selectTotalCountArticle(name);
        Map map=new HashMap();
        map.put("data",articles);
        map.put("count",count);
        map.put("msg","");
        map.put("code",0);
        return map;
    }

    @Override
    @DeleteCacheAnnotation
    public Map insertArticle(Article article) {
        Map map=new HashMap();

        int count=articleDao.insertArticle(article);
        if(count>0){
            map.put("isInsert",true);
        }else {
            map.put("isInsert",false);
        }
        map.put("isInsert",true);

        return map;

    }

    @Override
    @DeleteCacheAnnotation
    public Map updateArticle(Article article) {
        Map map=new HashMap();


        int count = articleDao.updateArticle(article);
        if(count>0){
            map.put("isUpdate",true);
        }else {
            map.put("isUpdate",false);
        }
        return map;
    }

    @Override
    @AddCacheAnnotation
    public Article selectOne(Integer id) {
        return articleDao.selectOne(id);
    }

    @Override
    @DeleteCacheAnnotation
    public Map deleteArticle(Integer id) {
        Map map=new HashMap();


        int count = articleDao.deleteArticle(id);
        if(count>0){
            map.put("isDelete",true);
        }else {
            map.put("isDelete",false);
        }
        return map;
    }
}
